﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Npgsql;
using System.Data;

namespace SIGEB.Modelo.moduloDocumentos
{
    public class conjuntoAutores : conjuntoBase
    {
        #region "constructores"

        public conjuntoAutores(datosAplicacion dA)
            : base(dA)
        {
            numTipo = conjuntoBase.CONJAUTORES;
        }

        #endregion

        #region "metodos"

        public override object obtenerEstado()
        {
            throw new NotImplementedException();
        }

        #region "imec"

        public void agregarautor(autor nuevoAutor)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT registrar_autor('" + nuevoAutor.nombre + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al agregar autor: " + nuevoAutor.nombre);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public void eliminarautor(String codautor)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT eliminar_autor('" + codautor + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al eliminar autor: " + codautor);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public autor obtenerautor(String codautor)
        {
            autor nuevo = null;
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_autor_id('" + codautor + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 1);
                    nuevo = new autor(valores[0]);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el autor " + codautor);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            return nuevo;
        }

        public LinkedList<autor> obtenerAutores()
        {
            LinkedList<autor> nuevo = new LinkedList<autor>();
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_autor()";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 1);
                    nuevo.AddLast(new autor(valores[0]));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el autor");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            return nuevo;
        }

        #endregion

        #endregion
    }
}
